package com.bj58.ecdata.dashboard.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.ServletRequestUtils;

import com.bj58.ecdata.calc.dbservice.service.BusslineEnum;
import com.bj58.ecdata.calc.dbservice.service.PlatformEnum;
import com.bj58.ecdata.dashboard.constant.EnumCate;
import com.bj58.ecdata.dashboard.databll.PriviledgeBLL;
import com.bj58.ecdata.dashboard.databll.TrackServiceBLL_TMP;
import com.bj58.ecdata.dashboard.service.SCFClient;
import com.bj58.ecdata.dashboard.utils.Option;
import com.bj58.ecdata.dashboard.utils.VelocityTools;
import com.bj58.groupbuy.action.interceptors.Check;
import com.bj58.groupbuy.action.model.User;
import com.bj58.groupbuy.action.util.SomePublicUtil;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.GET;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;


/**
 * 新旧机房流量实时监控看板
 * @author zhaoxiang 2015-10-10
 */
@SuppressWarnings("deprecation")
@Path("/realtimeTMP")
@Deprecated
public class NetFlowRealTime_TMP_Controller extends MvcController{
	private static final Log log = LogFactory.getLog(NetFlowRealTimeController.class);
	private final SomePublicUtil spu = new SomePublicUtil();
	private static final List<Option> busslineList = new ArrayList<Option>();
	private static final List<Option> platList = new ArrayList<Option>();
	private static final VelocityTools vt = new VelocityTools();

	{
		SCFClient.initialSCF();
		// 业务线选择
		busslineList.add(new Option("房产", "fangchan"));
		busslineList.add(new Option("黄页", "huangye"));
		busslineList.add(new Option("招聘", "zhaopin"));
		busslineList.add(new Option("二手", "ershou"));
		busslineList.add(new Option("二手车", "ershouche"));
		// 平台选择
		platList.add(new Option("PC", "PC"));
		platList.add(new Option("M", "M"));
		platList.add(new Option("APP", "APP"));
	}

	@Path("/netflow")
	@Check("dashboard/realtimeTMP/netflow")
	@POST
	@GET
	public ActionResult netFlowDashboard() {
		Map<String, Object> context = beat.getModel().getModel();
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			// 检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			if ("total".equals(businessOpers[0])) {
				businessOpers[0] = "";
			}
			String bussline_s = beat.getRequest().getParameter("bussline");
			String platform_s = beat.getRequest().getParameter("platform");

			BusslineEnum bussline = null;
			PlatformEnum platform = null;
			bussline = StringUtils.isBlank(bussline_s) ? null : BusslineEnum.valueOf(bussline_s);
			platform = StringUtils.isBlank(platform_s) ? null : PlatformEnum.valueOf(platform_s);

			context.put("bussline", bussline == null ? "" : bussline.value());
			context.put("platform", platform == null ? "" : platform.name());
			context.put("busslineList", businessOpers);
			context.put("business_CN_Map", EnumCate.business_CN_Map);
			context.put("platList", platList);
			context.put("vt", vt);
			// pv vppv
			JSONObject cashcost = JSONObject.fromObject(TrackServiceBLL_TMP.getDailySum(platform, bussline,null));
			context.put("pv", cashcost.get("pv"));
			context.put("vppv", cashcost.get("vppv"));

			return ActionResult.view("realtime/netflow_tmp");
		} catch (Exception e) {
			log.info(e);
			return ViewResult.jsonView("");
		}
	}

	@POST
	@Path("/netflow/getPvVppv")
	public ActionResult getPvVppv() {
		HttpServletRequest request = beat.getRequest();
		String bussline_s = request.getParameter("bussline");
		String platform_s = request.getParameter("platform");
		String tag_s = ServletRequestUtils.getStringParameter(request,"tag",null);

		BusslineEnum bussline = null;
		PlatformEnum platform = null;
		try {
			bussline = StringUtils.isBlank(bussline_s) ? null : BusslineEnum.valueOf(bussline_s);
			platform = StringUtils.isBlank(platform_s) ? null : PlatformEnum.valueOf(platform_s);
		} catch (Exception e) {
			log.info(e);
		}
		return ViewResult.jsonView(TrackServiceBLL_TMP.getDailySum(platform, bussline,tag_s));
	}

	/**
	 * 按秒获取流量数据（2秒内的数据）
	 * 
	 * @return
	 */
	@POST
	@Path("/netflow/getHourData")
	public ActionResult getCPCHourData() {
		HttpServletRequest request = beat.getRequest();
		String type_s = request.getParameter("type");
		String bussline_s = request.getParameter("bussline");
		String platform_s = request.getParameter("platform");
		String tag_s = ServletRequestUtils.getStringParameter(request,"tag",null);
		
		String time_s = beat.getRequest().getParameter("time");
		try {
			BusslineEnum bussline = StringUtils.isBlank(bussline_s) ? null : BusslineEnum.valueOf(bussline_s);
			PlatformEnum platform = StringUtils.isBlank(platform_s) ? null : PlatformEnum.valueOf(platform_s);
			long time = NumberUtils.toLong(time_s, 0);

			return ViewResult.jsonpView(TrackServiceBLL_TMP.getData(type_s, platform, bussline,tag_s, time));

		} catch (Exception e) {
		}

		return ViewResult.jsonView("{}");
	}

	@POST
	@Path("/netflow/getDayData")
	public ActionResult getCPCDayData() {
		HttpServletRequest request = beat.getRequest();
		String type_s = request.getParameter("type");
		String bussline_s = request.getParameter("bussline");
		String platform_s = request.getParameter("platform");
		String tag_s = ServletRequestUtils.getStringParameter(request,"tag",null);


		try {
			BusslineEnum bussline = StringUtils.isBlank(bussline_s) ? null : BusslineEnum.valueOf(bussline_s);
			PlatformEnum platform = StringUtils.isBlank(platform_s) ? null : PlatformEnum.valueOf(platform_s);
			JSONObject yes = JSONObject.fromObject(TrackServiceBLL_TMP.getDataYesterdayHourly(type_s, platform, bussline,tag_s));
			JSONObject now = JSONObject.fromObject(TrackServiceBLL_TMP.getDataTodayHourly(type_s, platform, bussline,tag_s));
			JSONObject totalData = JSONObject.fromObject(TrackServiceBLL_TMP.getDataTodayHourly(type_s, platform, bussline,null));
			now.element("yesdata", yes.get("data"));
			now.element("allData", totalData.get("data"));
			return ViewResult.jsonpView(now.toString());

		} catch (Exception e) {
		}
		return ViewResult.jsonView("{}");
	}


}
